# 
# breadcrumbs_helpers.rb
# 
# Created on 24/08/2007, 16:24:10

# Methods to be called on controller helpers and views.
# Returns the links to every linked controller (breadcrumbs)

module AutoRest
  module BreadcrumbsHelpers
    
    def ar_breadcrumbs
      return if not ActionController::Base.ar_has_controller_on_chain? @controller
      #return if !ardata.methods.include? request.path_parameters[:action].intern
         
      # Initializing Result
      # AKA: Put prefix or not? :)
      if ardata.breadcrumbs.prefix_label
        if ardata.breadcrumbs.prefix_controller
          url= url_for(:controller => ardata.breadcrumbs.prefix_controller)
          result= link_to ardata.breadcrumbs.prefix_label, url
        else
          result= ardata.breadcrumbs.prefix_label
        end
        result+= ardata.breadcrumbs.separator
      else
        result= ""
      end
      
      controller_chain= ActionController::Base.ar_controller_chain @controller, request.parameters
      
      ############
      # Pass One # Get the resources. We go in natural order so we can 
      ############   get the parent resource given the current one.

      resources= {}
      last_controller= nil

      controller_chain.each_with_index do |controller, iteration|
        
        controller_data= controller.ardata
        resource_name= controller_data.resource.singular.intern

        # If present, get the resource from the instance variable of the controller.
        # On First Iteration can get nil: ie: listing children of a parent, where no child is selected.
        resources[controller]= instance_variable_get("@#{resource_name.to_s}")

        # If resource is not present get the resource from the AR relation. 
        # I get into account the iteration because on top level could happen 
        # that no child is selected.
        if iteration > 0 && !resources[controller]
          if resources[last_controller]
            resources[controller]= resources[last_controller].send(resource_name)
          else
            result+= ardata.labels[:breadcrumbs_missing_resource]
          end
        end

        last_controller= controller
      end
      
      ############   
      # Pass Two # With the resources on place, build the proper breadcrumbs.
      ############   
      
      parent_controller= nil
      
      controller_chain.reverse_each do |controller|
        
        controller_data= controller.ardata
        
        # All resources link
        result+= ar_build_index_link controller_data, resources[parent_controller]

        # Selected link or new resource label.
        if resources[controller]
          if resources[controller].new_record?
            result+= content_tag(:strong, controller_data.breadcrumbs.label_new) + controller_data.breadcrumbs.separator
          else
            result+= ar_build_show_link controller_data, resources[parent_controller], resources[controller]
          end
        end
        
        parent_controller= controller
      end

      result
    end

    # Builds a link to index page of the resources
    # Needs the controller data and the parent_resource, wich can be null in case
    # of the root Controller
    def ar_build_index_link data, resource
      plural_label= data.breadcrumbs.plural
      meth= "#{data.resources_method(resource, '')}_url"
      plural_url= resource ? send(meth, resource) : send(meth)
      "#{link_to(plural_label, plural_url)}#{data.breadcrumbs.separator}"
    end

    # Builds a link to show page of the resources
    def ar_build_show_link data, parent_resource, resource
      singular_label= content_tag :strong, "#{data.breadcrumbs.singular}#{resource.label}"
      meth= "#{data.resource_method(parent_resource, '')}_url"
      singular_url= parent_resource ? send(meth, parent_resource, resource) : send(meth, resource)
      "#{link_to(singular_label, singular_url)}#{data.breadcrumbs.separator}"
    end
  end
end
